#-------------------------------------------------------------------------------
#!	\file		default.awk
#!
#!	\brief		Extract/build default DMAC configuration from C header file.
#!
#!	\details
#!				This awk script extracts defines from a C header file
#!				and creates a default C file for the DMA controller.
#!				It can be used by the driver to initialize the devivce
#!
#!	\author		ACV
#!
#!	\version	1
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#	Before the file is processed:
#	- Create the lua file name from the c header
#	- Start the lua script
#-------------------------------------------------------------------------------
BEGIN {
	first = 1
	base = 0
	split(ARGV[1],in_name,".")
	out_name = in_name[1] "_default.c"

	printf("//-------------------------------------------------------------------\n") > out_name
	printf("// %s default registers\n",in_name[1]) > out_name
	printf("// File extracted from: %s\n",ARGV[1]) > out_name
	printf("// Generation Date: %s\n",strftime()) > out_name
	printf("//\n") > out_name
	printf("// This is a generated file.  It is created by default.awk.\n") > out_name
	printf("// Do not edit.\n") > out_name
	printf("//-------------------------------------------------------------------\n") > out_name
	printf("#include <stdint.h>\n") > out_name
	printf("#include <stdbool.h>\n\n") > out_name
	printf("#include <dmac.h>\n\n") > out_name
	printf("%s_default_t d[] = {\n",in_name[1]) > out_name
}

#-------------------------------------------------------------------------------
#	Toss out comments
#-------------------------------------------------------------------------------
$1 ~/^\/\// {
#	print "comment:" $0
	next
}

#-------------------------------------------------------------------------------
#	When a define is matched:
#	- extract the symbol and the value
#	- write out a low and high register definition to the Lua table
#-------------------------------------------------------------------------------
$1 ~/^#define/ {
	sym = $2
	val = strtonum($3)

	if (first == 1) {
		base = val
		first = 0
	}
	else {
		reg_lo = $2 "_low"
		reg_hi = $2 "_high"
		printf("\t{ 0x%08X,\t0x00000000 },\t\t// %-48s\n", base+val,	reg_lo) > out_name
		printf("\t{ 0x%08X,\t0x00000000 },\t\t// %-48s\n", base+val+4,	reg_hi) > out_name
	}
}

#-------------------------------------------------------------------------------
#	At the end of the file:
#	- Write out the end of the lua table
#	- close out the Lua file
#-------------------------------------------------------------------------------
END {
	printf("};\n") > out_name
	close(out_name)
}

#
#	End: extract.awk
#
